package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.CorporationBean;
import com.attendance.www.hr.dao.mapper.CorporationExtMapper;
import com.attendance.www.hr.dao.mapper.CorporationMapper;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationMapper;
import com.attendance.www.hr.entitys.Corporation;
import com.attendance.www.hr.entitys.CorporationExample;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.service.ICorporationService;

@Service
public class CorporationServiceImpl implements ICorporationService {
	@Resource
	private CorporationMapper CorporationDao;
	@Resource
	private CorporationExtMapper CorporationExtDao;
	@Resource
	private EmployeeCorporationMapper employeeCorporationDao;
	@Resource
	private DepartmentMapper departmentDao;
	@Resource
	private CorporationExtMapper corporationExtDao;

	/**
	 * 添加公司
	 */
	@Override
	public boolean saveCorporation(CorporationBean corporationBean) {
		Corporation corporation=new Corporation();
		corporation.setCorporationName(corporationBean.getCorporationName());
		corporation.setDescription(corporationBean.getDescription());
		corporation.setCorporationCode(corporationBean.getCorporationCode());
		corporation.setFromDate(new Date());
		int record = CorporationDao.insertSelective(corporation);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取公司
	 */
	@Override
	public List<CorporationBean> getCorporationList(CorporationBean corporationBean) {
		return CorporationExtDao.getCorporationList(corporationBean);
	}

	/**
	 * 修改公司
	 */
	@Override
	public boolean updateCorporation(CorporationBean corporationBean) {
		Corporation corporation=new Corporation();
		corporation.setCorporationId(corporationBean.getCorporationId());
		corporation.setCorporationCode(corporationBean.getCorporationCode());
		corporation.setCorporationName(corporationBean.getCorporationName());
		if(corporationBean.getDescription()==null) {
			corporation.setDescription(" ");
		}
		corporation.setDescription(corporationBean.getDescription());
		corporation.setUpdateTime(new Date());
		int record = CorporationDao.updateByPrimaryKeySelective(corporation);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除公司
	 */
	@Override
	public boolean deleteCorporation(Integer corporationId) {
		Corporation corporation=new Corporation();
		corporation.setCorporationId(corporationId);
		corporation.setIsDelete(1);
		int record = CorporationDao.updateByPrimaryKeySelective(corporation);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据corporationId获取一条数据
	 */
	@Override
	public Corporation getCorporationByCorporationId(Integer corporationId) {
		
		return CorporationDao.selectByPrimaryKey(corporationId);
	}

	/**
	 * 根据corporation签到时间判断数据库里是否有相同的数据
	 */
	@Override
	public boolean getCorporationByCorporation(CorporationBean corporationBean) {
		CorporationExample example=new CorporationExample();
		CorporationExample.Criteria criteria = example.createCriteria();
		criteria.andCorporationCodeEqualTo(corporationBean.getCorporationCode());
		if (corporationBean.getCorporationId()!=null) {
			criteria.andCorporationIdNotEqualTo(corporationBean.getCorporationId());
		}
		criteria.andIsDeleteEqualTo(0);
		List<Corporation> corporationList=CorporationDao.selectByExample(example);
		if(corporationList.size()>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 查询公司下有没有部门--zx
	 */
	@Override
	public boolean getCorporationDepartment(Integer corporationId) {
		DepartmentExample example=new DepartmentExample();
		example.createCriteria().andCorporationIdEqualTo(corporationId);
		List<Department> list=departmentDao.selectByExample(example);
		if (list.size()>0) {
			return true;
		}
		return false;
	}

	/**
	 * 根据人查其被分配的HR权限公司
	 */
	@Override
	public List<CorporationBean> getCorporationPermitList(CorporationBean corporationBean) {
		corporationBean.setFromDate(new Date());
		return corporationExtDao.getCorporationPermitList(corporationBean);
	}

}
